package com.zh.test.controller;

import io.swagger.models.auth.In;
import org.apache.poi.ss.usermodel.*;
import org.junit.Test;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@RestController
@RequestMapping("/excel")
public class excelController {

    public static void main(String[] args) {
        //String old  = "收王娟、李凯强7#-3-301首付款_王娟_2021";
        //String old  = "收王娟、李凯强7#-3-301首付款_王娟2021";
        //String old  = "收王娟、李凯强7#-3-301首付款_王娟 李凯强";
        //String old  = "收王娟、李凯强7#-3-301首付款_王娟、 李凯强";
        //String old  = "收王娟、李凯强7#-3-301首付款_王娟李凯强";
        String old  = "收王平18-2-502首付款_邸西友王平18-2-502_";
        String s = analysisName(old);
        //excelRead();
    }

    @GetMapping("/read")
    public static void excelRead() {
        InputStream inp = null;
        try {
            inp = new FileInputStream("C:\\Users\\zh\\Desktop\\excel\\test20210121.xlsx");
            Workbook wb = WorkbookFactory.create(inp);
            Sheet sheet = wb.getSheetAt(0);
            ArrayList<Integer> errorList = new ArrayList<>();
            int count = 0;
            ArrayList<Map<String, Object>> successList = new ArrayList<>();
            HashMap<String,Integer> successMap = new HashMap<>();
            for (int i = 1; i < sheet.getLastRowNum(); i++) {
                Row row = sheet.getRow(i);
                Cell c4 = row.getCell(4);
                String name = analysisName(c4.getStringCellValue());
                Cell c6 = row.getCell(6);
                Integer money =  (int)c6.getNumericCellValue();
                if ("underfind".equals(name)) {
                    errorList.add(i);
                    continue;
                }
                if(successMap.containsKey(name)){
                    successMap.put(name,successMap.get(name)+money);
                }else{
                    successMap.put(name,money);
                }




//                HashMap<String, Object> data = new HashMap();
//                if (successList.stream().anyMatch(mm -> mm.get("name").equals(name))) {
//                    successList.stream().map(mm -> {
//                        mm.get("name").equals(name);
//                        return mm;
//                    }).forEach(t -> {
//                        data.put("money", (int) t.get("money") + (int) row.getCell(5).getNumericCellValue());
//                    });
//                } else {
//                    data.put("money", (int) row.getCell(5).getNumericCellValue());
//                }
//                data.put("name", name);
//                successList.add(data);
                System.out.println("姓名:" + name + "     金额:" + successMap.get(name));
                count++;
            }
            System.out.println("excel中" + count + "数据,解析结果为" + successMap.size() + "    解析失败" + errorList.size());

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //  inp.close();
        }
    }

    @Test
    public static String analysisName(String old) {
        //String old  = "收王娟、李凯强7#-3-301首付款_王娟_2021";
        //String old  = "收王娟、李凯强7#-3-301首付款_王娟2021";
        //String old  = "收王娟、李凯强7#-3-301首付款_王娟 李凯"
        //String old  = "收王娟、李凯强7#-3-301首付款_王娟、 李凯强";
        //String old  = "收王娟、李凯强7#-3-301首付款_王娟李凯强";
        //String old  = "收王平18-2-502首付款_邸西友王平18-2-502_"
        String pattern_ = "_[\\u4E00-\\u9FA5]+_?|_[\\u4E00-\\u9FA5]+\\d";
        String patternAnd = "_[\\u4E00-\\u9FA5]+(\\s|、)*[\\u4E00-\\u9FA5]*";
        Pattern r_ = Pattern.compile(pattern_);
        Pattern rAnd = Pattern.compile(patternAnd);
        Matcher m_ = r_.matcher(old);
        Matcher mAnd = rAnd.matcher(old);
        if (m_.find()) {
            String name = m_.group(0);
            return name.substring(1, name.length() - 1);
        }
        if (mAnd.find()) {
            String name = mAnd.group(0);
            String[] split = name.split("(\\s|、)+");
            return split[0].substring(1, split[0].length()) + split[1];
        }
        return "underfind";
    }

    public String money(String old) {

        return old.substring(0, old.length() - 1);
    }


}
